<template>
  <div class="tranLogisTics">
    <el-form :model="tranLogForm" :rules="tranLogRules" v-loading="tranLogLoading" label-width="100px" ref="tranLogRefs" class="tranLogCls">
      <el-form-item label="编码：" prop="code">
        <el-input v-model="tranLogForm.code" type="text" size="small" placeholder="请输入编码"></el-input>
      </el-form-item>
      <el-form-item label="物流名称：" prop="name">
        <el-input v-model="tranLogForm.name" type="text" size="small" placeholder="请输入物流公司名称"></el-input>
      </el-form-item>
      <el-form-item label="排序：" prop="sort">
        <el-input-number v-model="tranLogForm.sort" :min="1" :setp="1" size="small" placeholder="排序" @keydown.native="channelInputLimit" />
      </el-form-item>
      <el-form-item label="是否启用：" prop="isShow">
        <el-radio-group v-model="tranLogForm.isShow">
          <el-radio label="1">开启</el-radio>
          <el-radio label="0">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <div class="subBtnWid">
        <el-button size="mini" type="primary" @click="sub('tranLogRefs')" :loading="loadingBtn">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
  import {
    getLogisticsInfo as getTranInfo,
    postLogisticsInfo as postTranInfo,
    putLogistics as putTranInfo
  } from '@/api/setting';

  export default {
    name: 'Index',
    props: {
      isEdit: {
        type: [String, Number],
        default: 0
      },
      id: {
        type: [String, Number],
        default: 0
      }
    },
    data() {
      return {
        tranLogForm: {
          code: '',
          name: '',
          sort: 1,
          isShow: '1',
          id: 0
        },
        tranLogRules: {
          code: [
            { required: true, message: '请填写编码', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请填写物流名称', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '请填写排序', trigger: 'change' }
          ],
          isShow: [
            { required: true, message: '请选择是否启用', trigger: 'change' }
          ]
        },
        tranLogLoading: false,
        loadingBtn: false
      };
    },
    mounted() {
      if (this.isEdit == 1 && this.id) this.getList();
    },
    methods: {
      sub(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$set(this, 'loadingBtn', true);
            if (this.id && this.isEdit == 1) {
              putTranInfo({
                code: this.tranLogForm.code,
                name: this.tranLogForm.name,
                sort: this.tranLogForm.sort,
                isShow: Number(this.tranLogForm.isShow)
              }, this.id).then(() => {
                this.$message.success('编辑成功');
                this.$emit('getTranList');
                this.$emit('close');
                this.$set(this, 'loadingBtn', false);
              }).catch(() => {
                this.$set(this, 'loadingBtn', false);
              });
            } else {
              postTranInfo({
                code: this.tranLogForm.code,
                name: this.tranLogForm.name,
                sort: this.tranLogForm.sort,
                isShow: Number(this.tranLogForm.isShow)
              }).then(() => {
                this.$message.success('新增成功');
                this.$emit('getTranList');
                this.$emit('close');
                this.$set(this, 'loadingBtn', false);
              }).catch(() => {
                this.$set(this, 'loadingBtn', false);
              });
            };
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      getList() {
        this.$set(this, 'tranLogLoading', true);
        getTranInfo(this.id).then(res => {
          this.$set(this, 'tranLogForm', {
            code: res.code,
            name: res.name,
            sort: res.sort,
            isShow: res.isShow.toString(),
            id: res.id
          });
          this.$set(this, 'tranLogLoading', false);
        }).catch(() => {
          this.$set(this, 'tranLogForm', {});
          this.$set(this, 'tranLogLoading', false);
        });
      },
      channelInputLimit(e) {
        const key = e.key
        if (key === 'e' || key === '.') {
          e.returnValue = false
          return false
        }
        return true
      }
    }
  }
</script>

<style scoped>

</style>
